var skinmap = {
	"1":"http://img03.taobaocdn.com/imgextra/i3/137009487/T2voFIXa8aXXXXXXXX_!!137009487.jpg",
	"2":"http://img03.taobaocdn.com/imgextra/i3/137009487/T26EFIXnJXXXXXXXXX_!!137009487.jpg",
	"3":"http://img03.taobaocdn.com/imgextra/i3/137009487/T2i54lXnFMXXXXXXXX_!!137009487.jpg",
	"4":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2754lXk0MXXXXXXXX_!!137009487.jpg",
	"5":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2JoFIXX0aXXXXXXXX_!!137009487.jpg",
	"6":"http://img04.taobaocdn.com/imgextra/i4/137009487/T2UoBIXdVaXXXXXXXX_!!137009487.jpg",
	"7":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2ozXlXl4MXXXXXXXX_!!137009487.jpg"
};

function display_sub_rule() {
  var rule_type=$('#hidden_rule_type').val();
  if(rule_type=='TF_M_USER_RULE_TEMP'){
    load_gridtable_temp(rule_type);
  }else if(rule_type=='TF_M_USER_RULE_AREA'){
    load_gridtable_area(rule_type);
  }else if(rule_type=='TF_M_USER_RULE_DATETIME'){
    load_gridtable_datetime(rule_type);
  }else if(rule_type=='TF_M_USER_RULE_TIME'){
    load_gridtable_time(rule_type);
  }else if(rule_type=='TF_M_USER_RULE_RANDOM'){
    load_gridtable_random(rule_type);
  }
}

function load_gridtable_temp(rule_type) {
  $("#gridTable").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 810,
    colNames: ["编号", "温度范围", "标题", "一行显示", "使用皮肤","当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      hidden: true
    },
    {
      name: "TEMP_RANGE",
      index: "TEMP_RANGE",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formateTemp
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateadorLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      var result  =$("#gridTable").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "按温度显示"
  });
}

function load_gridtable_area(rule_type) {
  $("#gridTable").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 810,
    colNames: ["编号", "地区", "标题", "一行显示", "使用皮肤","当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      hidden: true
    },
    {
      name: "AREA_NAMES",
      index: "AREA_NAMES",
      width: "200"
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200",
      sortable: false
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateadorLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      var result  =$("#gridTable").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "按地区显示"
  });
}


function load_gridtable_datetime(rule_type) {
  $("#gridTable").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 450,
    width: 810,
    colNames: ["编号", "标题", "生效时间", "失效时间", "一行显示", "使用皮肤", "当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      hidden:true
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "START_TIME",
      index: "START_TIME",
      width: "200"
    },
    {
      name: "END_TIME",
      index: "END_TIME",
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateadorLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      var result  =$("#gridTable").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "按日期显示"
  });
}

function load_gridtable_time(rule_type) {
  $("#gridTable").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 450,
    width: 810,
    colNames: ["编号", "起止时间段", "标题", "一行显示", "使用皮肤", "当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      hidden: true
    },
    {
      name: "TIME_RANGE",
      index: "TIME_RANGE",
      width: "160",
      align:"center",
      sortable: false,
      formatter:formateTime
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "100",
      align:"center",      
      formatter:formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      align:"center",       
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateadorLink,
      width: "70"
    } 
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      var result  =$("#gridTable").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "按时间段显示"
  });
}

function load_gridtable_random(rule_type) {
  $("#gridTable").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 810,
    colNames: ["编号", "标题", "一行显示", "使用皮肤","当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      hidden: true
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateadorLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      var result  =$("#gridTable").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "按温度显示"
  });
}

function setImageCellHeight() { 
    var ids = $("#gridTable").getDataIDs(); 
    for (var i = 0; i < ids.length; i++) { 
      var cl = ids[i]; 
      $("#gridTable").setRowData(ids[i],false, {height: 40}); 
    } 
}

function deleteUserRuleSubByButton(){
  var rowids=$("#gridTable").jqGrid('getGridParam','selarrrow');
  if(rowids==''){
    alert("请在下表中选择要删除的规则");
    return;
  }
  
  rowidArray = (''+rowids).split(",");
  
  var rule_ids='';
  for(var i = 0;i<rowidArray.length;i++){
    var row_data = $("#gridTable").jqGrid("getRowData", rowidArray[i]);
    rule_ids+=row_data.RULE_ID+',';
  }
  rule_ids = rule_ids.slice(0,rule_ids.length-1);
  deleteUserRuleSub(rule_ids);
}

function deleteUserRuleSubByLink(rule_id){
  deleteUserRuleSub(rule_id);
}

function deleteUserRuleSub(rule_ids){
  var params={
    ruleIds:rule_ids,
    ruleType:$('#hidden_rule_type').val()
  }
  $.post("delete_rule_sub.action",params,delete_rule_sub_response,"json");
}

function delete_rule_sub_response(data){
  if(validate_response_result(data.result)){
    $("#gridTable").trigger("reloadGrid");
  }
}

function validate_response_result(result){
  if(Number(result.code<0)){
    alert(result.msg);
    return false;
  }
  return true;;
}

function formateadorLink(cellvalue, options, rowObject) {
   var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>删除</span>"
   return "<a href='javascript:deleteUserRuleSubByLink("+rowObject.RULE_ID+");'>" + title + "</a>";
}


/*
*暂时没有修改功能，这个函数没有用到
*/
function formateModifyLink(cellvalue, options, rowObject) {
   var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>修改</span>"
   var rule_type = $('#hidden_rule_type').val();
   var href = $('#modify_user_rule_id').val()+"?ruleType="+rule_type+"&ruleId="+rowObject.RULE_ID;
   return "<a href='"+href+"' target='_blank'>" + title + "</a>";
}

function formateTemp(cellvalue, options, rowObject) {
   return rowObject.START_TEMP+"℃~"+rowObject.END_TEMP+"℃";
}

function formateTime(cellvalue, options, rowObject) {
   return rowObject.S_TIME+"~"+rowObject.E_TIME;
}

function formateNumPerTr(cellvalue, options, rowObject) {
   return cellvalue+"个宝贝";
}
function formatSkinImg(cellvalue, options, rowObject) {
	return "<img src='"+skinmap[cellvalue]+"' height='25' width='80'></img>";
}
var frdmap = {
	"TF_M_USER_RULE_AREA":"/jsp/rule_settings/area/area.jsp",
	"TF_M_USER_RULE_DATETIME":"/jsp/rule_settings/datetime/datetime.jsp",
	"TF_M_USER_RULE_TIME":"/jsp/rule_settings/time/time.jsp",
	"TF_M_USER_RULE_TEMP":"/jsp/rule_settings/temperature/temperature.jsp"
};
